import { Base64 } from 'js-base64'

console.log('重写localStorage加载')
localStorage.setItem = (function (oriLogFunc) {
  return function () {
    try {
      if (arguments[1] !== null && arguments[1] !== '') {
        console.log('加密key：' + arguments[0])
        console.log('加密前：' + arguments[1])
        arguments[1] = Base64.encode(arguments[1].toString())
        console.log('加密后：' + arguments[1])
      }
      oriLogFunc.call(localStorage, ...arguments)
    } catch (e) {
      console.error('localStorage.setItem error', e)
    }
  }
})(localStorage.setItem)
localStorage.getItem = (function (oriLogFunc) {
  return function () {
    try {
      let result = oriLogFunc.call(localStorage, ...arguments)
      if (result !== null && result !== '') {
        // console.log('解密前：' + result)
        result = Base64.decode(result)
        // console.log('解密后：' + result)
      }
      return result
    } catch (e) {
      console.error('localStorage.getItem error', e)
    }
  }
})(localStorage.getItem)
localStorage.clear = (function (oriLogFunc) {
  return function () {
    try {
      console.log('重写clear生效')
      oriLogFunc.call(localStorage, ...arguments)
    } catch (e) {
      console.error('localStorage.clear error', e)
    }
  }
})(localStorage.clear)
